package cn.interestingshop.dao;

import cn.interestingshop.entity.Goods;
import cn.interestingshop.entity.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl extends BaseDaoImpl implements GoodsDao {

    public GoodsDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public Goods createEntityByResultSet(ResultSet rs) throws Exception {

        Goods goods = new Goods();

        if (rs.next()) {
            goods.setId(rs.getInt("id"));
            goods.setStock(rs.getInt("stock"));
            goods.setPrice(rs.getFloat("price"));
            goods.setGoodsName(rs.getString("goods_name"));
            goods.setGoodsDesc(rs.getString("goods_desc"));
            goods.setFileName(rs.getString("file_name"));
            goods.setClassifyLevel3Id(rs.getInt("classify_level3_id"));
            goods.setClassifyLevel2Id(rs.getInt("classify_level2_id"));
            goods.setClassifyLevel1Id(rs.getInt("classify_level1_id"));
        }
        return goods;
    }

    @Override
    public Goods selectOne(Integer id) {

        ResultSet rs = null;

        String sql = "select id,goods_name,goods_desc,price,stock,classify_level1_id,classify_level2_id,classify_level3_id,file_name,is_delete from t_goods where id=" + id;
        rs = executeQuery(sql, null);
        Goods goods = new Goods();
        try {
            goods = createEntityByResultSet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource();
            closeResource(rs);
        }
        return goods;
    }

    public void goodsList(ResultSet rs, List<Goods> goodsList) throws Exception {


        while (rs.next()) {


            Goods goods = new Goods();

            goods.setId(rs.getInt("b.id"));
            goods.setClassifyLevel1Id(rs.getInt("b.classify_level1_id"));
            goods.setClassifyLevel2Id(rs.getInt("b.classify_level2_id"));
            goods.setClassifyLevel3Id(rs.getInt("b.classify_level3_id"));
            goods.setFileName(rs.getString("b.file_name"));
            goods.setGoodsDesc(rs.getString("b.goods_desc"));
            goods.setGoodsName(rs.getString("b.goods_name"));
            goods.setPrice(rs.getFloat("b.price"));
            goods.setStock(rs.getInt("b.stock"));

            goodsList.add(goods);
        }


    }


    @Override
    public List<Goods> selectAll(String keyWord) {

        ResultSet rs = null;
        ResultSet rs2 = null;
        ResultSet rs3 = null;
        ResultSet rs4 = null;

        List<Goods> goods = new ArrayList<>();

        String sql1 = "select b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_goods b where b.goods_name like '%" + keyWord + "%'";
        Object[] objects = new Object[]{keyWord};

        rs = executeQuery(sql1, null);


        String sql2 = "select a.id,a.classify_name,a.parent_id,a.type,a.icon,b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_classify a,t_goods b where  a.id=b.classify_level1_id and a.classify_name=?";
        rs2 = executeQuery(sql2, objects);

        String sql3 = "select a.id,a.classify_name,a.parent_id,a.type,a.icon,b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_classify a,t_goods b where  a.id=b.classify_level2_id and a.classify_name=?";
        rs3 = executeQuery(sql3, objects);

        String sql4 = "select a.id,a.classify_name,a.parent_id,a.type,a.icon,b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_classify a,t_goods b where  a.id=b.classify_level3_id and a.classify_name=?";
        rs4 = executeQuery(sql4, objects);

        try {
            goodsList(rs, goods);
            goodsList(rs2, goods);
            goodsList(rs3, goods);
            goodsList(rs4, goods);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource();
            closeResource(rs);
            closeResource(rs2);
            closeResource(rs3);
            closeResource(rs4);
        }


        return goods;
    }

    @Override
    public List<Goods> selectAll(Integer currentPageNo, Integer pageSize) {

        ResultSet rs=null;

        List<Goods> goods=new ArrayList<>();

        String sql="select b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_goods b ";
        sql += " limit " + (currentPageNo - 1) * pageSize + "," + pageSize;

        rs=executeQuery(sql,null);
        try {
            goodsList(rs,goods);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }


        return goods;
    }

    @Override
    public int findUserCount() {
        ResultSet rs=null;

        int i=0;


        String sql="select count(1) from t_goods ";

        rs=executeQuery(sql,null);
        try {
            if(rs.next()){
                i=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }


        return i;
    }


    @Override
    public int findUserCount(List<Goods> goods) {

        int rs = 0;

        for (Goods good : goods) {
            rs++;
        }


        return rs;
    }


    @Override
    public List<Goods> selectOne(Integer id, Integer type, Integer currentPageNo, Integer pageSize) {

        ResultSet rs = null;

        List<Goods> goods = new ArrayList<>();

        String sql1 = "select b.id,b.goods_name,b.goods_desc,b.price,b.stock,b.classify_level1_id,b.classify_level2_id,b.classify_level3_id,b.file_name,b.is_delete from t_classify a,t_goods b where  a.id=b.classify_level" + type + "_id  and a.id=" + id;
        sql1 += " limit " + (currentPageNo - 1) * pageSize + "," + pageSize;
        System.out.println(sql1);

        rs = executeQuery(sql1, null);

        try {
            goodsList(rs, goods);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource();
            closeResource(rs);
        }

        return goods;
    }

    @Override
    public int findUserCount(Integer id, Integer type) {
        ResultSet rs = null;

        int total = 0;

        String sql1 = "select count(1) from t_classify a,t_goods b where  a.id=b.classify_level" + type + "_id  and a.id=" + id;

        rs = executeQuery(sql1, null);

        try {
            if (rs.next()) {
                total = rs.getInt(1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource();
            closeResource(rs);
        }

        return total;
    }

    @Override
    public int kjKc(int id, int stock) {
        int total = 0;

        try {
            String sql1 = "update t_goods set stock=stock-? where id=?";

            Object[] objects = new Object[]{stock, id};

            total = executeUpdate(sql1, objects);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource();
        }

        return total;
    }

    @Override
    public int deleteById(int id) {

        int rs=0;

        try{
            String sql="delete from t_goods where id="+id;

            rs=executeUpdate(sql,null);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
        }



        return rs;
    }

    @Override
    public int executeUpdate(Goods goods) {
        int rs=0;

        try{
            String sql="";
            Object[] objects;
            if(goods.getFileName()!=null && goods.getFileName()!=""){
                sql="update t_goods set goods_name=?,goods_desc=?,price=?,stock=?,classify_level1_id=?,classify_level2_id=?,classify_level3_id=?,file_name=? where id=?";
                objects=new Object[]{goods.getGoodsName(),goods.getGoodsDesc(),goods.getPrice(),goods.getStock(),goods.getClassifyLevel1Id(),goods.getClassifyLevel2Id(),goods.getClassifyLevel3Id(),goods.getFileName(),goods.getId()};
            }else{
                sql="update t_goods set goods_name=?,goods_desc=?,price=?,stock=?,classify_level1_id=?,classify_level2_id=?,classify_level3_id=? where id=?";
                objects=new Object[]{goods.getGoodsName(),goods.getGoodsDesc(),goods.getPrice(),goods.getStock(),goods.getClassifyLevel1Id(),goods.getClassifyLevel2Id(),goods.getClassifyLevel3Id(),goods.getId()};
            }


            rs=executeUpdate(sql,objects);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
        }



        return rs;
    }

    @Override
    public int insert(Goods goods) {

        int rs=0;

        try{
            String sql="";
            Object[] objects;

                sql="INSERT INTO `interesting_shop_db`.`t_goods`(`goods_name`, `goods_desc`, `price`, `stock`, `classify_level1_id`, `classify_level2_id`, `classify_level3_id`, `file_name`) VALUES (?,?,?,?,?,?,?,?)";
                objects=new Object[]{goods.getGoodsName(),goods.getGoodsDesc(),goods.getPrice(),goods.getStock(),goods.getClassifyLevel1Id(),goods.getClassifyLevel2Id(),goods.getClassifyLevel3Id(),goods.getFileName()};



            rs=executeUpdate(sql,objects);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
        }



        return rs;
    }

}
